//全局变量
var userId;



$(function(){
	queryUser(1);
	//全选
	$('#all').on("click",function(){
		$(".deletetr input").prop("checked", this.checked);
    });
	$(".deletetr input").on('click',function() {  
	      var $subs = $(".deletetr input");  
	      $("#all").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);  
	}); 
	//删除用户
	$("#delButton").click(function(){
		obj = document.getElementsByName("ck");
		ids = [0];
	    for(k in obj){
	        if(obj[k].checked)
	            ids.push(obj[k].value);
	    }
	    var flag = false;
	    if(ids.length != 1){
	    	var str = "确认删除id为"
	    	for (var i=1;i<ids.length;i++)
	    	{
	    		str = str + ids[i] + ",";
	    	}
	    	str = str.substring(0,str.length - 1);
	    	str = str + "的用户吗?"
	    	flag = confirm(str) ;
	    }
		if(flag){
			deleteUsers(ids);
		}
	});
	//更新用户
	$("#editButton").click(function(){
		obj = document.getElementsByName("ck");
		ids = [0];
	    for(k in obj){
	        if(obj[k].checked)
	            ids.push(obj[k].value);
	    }
	    if(ids.length > 2){
	    	alert("不好意思,只允许一次修改一个用户QAQ");
	    }else if(ids.length == 2){
	    	getUserInfo(ids);
	    }
	   	
	});
	//弹窗按钮
	$("#checkno").click(function(){
		$("#modal-form").attr("class","modal fade");
		$("#modal-form").attr("style","display: none");
	});
	$("#checkok").click(function(){
		return updateUser();
	});
	//新增用户
	$("#addButton").click(function(){
		
	});
});

function updateUser(){
	var userName = $("#fuserName").val();
	var phoneNum = $("#fphonenum").val();
	var email = $("#femail").val();
	var detail = $("#fdetail").val();
	if(!(/^1(3|4|5|7|8)\d{9}$/.test(phoneNum))){
		$("#phonev").attr("style","display: block;color:red;");
		return false;
	}else{
		$("#phonev").attr("style","display: none");
	}
	var emailreg = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/;
	if(!emailreg.test(email)){
		$("#emailv").attr("style","display: block;color:red;");
		return false;
	}else{
		$("#emailv").attr("style","display: none");
	}
	$.ajax({
		url:"/user/update",
		type:"post",
		data:{
			  "userId":userId,
			  "userName":userName,
			  "phoneNum":phoneNum,
			  "email":email,
			  "personSign":detail
			},
		dataType:"json",
		success:function(result){
			if(result.status == 200){
				$("#modal-form").attr("class","modal fade");
				$("#modal-form").attr("style","display: none");
				queryUser(1);
			}else{
				alert("更新失败");
			}
		},
		error:function(){
			alert("请求失败");
		}
	});
}

function getUserInfo(ids){
	userId = ids[1];
	$("#emailv").attr("style","display: none");
	$("#phonev").attr("style","display: none");
	$("#modal-form").attr("class","modal fade in");
	$("#modal-form").attr("style","display: block; padding-right: 6px;");
	var userName = $("input:checkbox:checked").parent().parent().find('th').eq(2).text();
	var phoneNum = $("input:checkbox:checked").parent().parent().find('th').eq(3).text();
	var email = $("input:checkbox:checked").parent().parent().find('th').eq(4).text();
	var detail = $("input:checkbox:checked").parent().parent().find('th').eq(7).text();
	$("#fuserName").val(userName);
	$("#fphonenum").val(phoneNum);
	$("#femail").val(email);
	$("#fdetail").val(detail == "该用户很懒什么都没有留下" ? null : detail);
	
}

function deleteUsers(ids){
	
    $.ajax({
    	url:"/user/delete/" + ids,
    	type:"delete",
    	dataType:"json",
    	success:function(result){
    		if(result.status == 200){
    			queryUser(1);
    		}
    	},
    	error:function(){
    		alert("请求失败");
    	}
    });
}


function queryUser(currentPage){
	$.ajax(
			{
		url:"/user/query",
		type:"get",
		data:{"currentPage":currentPage},
		dataType:"json",
		success:function(result){
			if(result.status == 200){
				var te = $(".deletetr").html("");
				$("#user_page").html("");
				var page = result.data;
				var users = page.data;
				$(users).each(function(n,user){
					var tr = '<tr class="deletetr">'+
						'<th>'+
					'<input type="checkbox" name="ck" value="'+user.userId+'">'+
				'</th>'+
				'<th>'+
					user.userId +
				'</th>'+
				'<th>'+
					user.userName+
				'</th>'+
				'<th>'+
					user.phoneNum+
				'</th>'+
				'<th>'+
					user.email+
				'</th>'+
				
				'<th>'+
					user.status+
				'</th>'+
				'<th>'+
				((user.personSign == null) ? "该用户很懒什么都没有留下" : user.personSign)
			'</th>'+
			'</tr>';
					$("#first").append(tr);
				});
				
				if(page.totalPage>1){
					//大于一页才需要显示分页条						
					//处理前一页
					var previous='<li>'+
				                    '<a href="javascript:queryUser('+page.previousPage+')" aria-label="Previous">'+
		                              '<span aria-hidden="true">&laquo;</span>'+
		                            '</a>'+
		                         '</li>';
					$("#user_page").append(previous);
					//处理中间的超链接
					$(page.aNum).each(function(n,value){
						var middle='<li><a href="javascript:queryUser('+value+')">'+value+'</a></li>';
						$("#user_page").append(middle);
					});
					
					
					//处理后一页
					var next='<li>'+
				               '<a href="javascript:queryUser('+page.nextPage+')" aria-label="Next">'+
		                          '<span aria-hidden="true">&raquo;</span>'+
		                       '</a>'+
		                     '</li>';
					$("#user_page").append(next);
				}
			}else{
				alert("查询失败");
			}
		},
		error:function(){
			alert("请求失败");
		}
		
		
	});
}